# -*- coding: utf8 -*-

from time import sleep
from threading import RLock

class Project( object ):

    _lock = None
    _manager = None

    tables = (
        "option", "directory", "file", "hash", "image",
        "image_vector10", "image_vector100",
        "image2image", "file2file",
    )

    def __init__( self, manager ):
        self._lock = RLock()
        self._manager = manager
        pass

    def getManager(self):
        return self._manager


    def getTable(self, name):
        self._lock.acquire()
        self.getManager().start_service(name)
        self._lock.release()
        return self.getManager().get_request()


    def create( self ):
        for name in self.tables:
            self.getTable(name).query(name, "create", {}, False)
        return None, True

    def clean( self ):
        for name in self.tables[1:]:
            self.getTable(name).query(name, "clean", (), False)
        return None, True

    def close( self ):
        for name in self.tables:
            self.getTable(name).query(name, "close", (), False)
        return None, True


    def save_option( self, name, value ):
        table = "option"
        req = self.getTable(table).query(table, "save_option", (name, value,), False)
        return table, req

    def get_option( self, name ):
        table = "option"
        req = self.getTable(table).query(table, "get_option", (name,), True)
        return table, req

    def get_options( self ):
        table = "option"
        req = self.getTable(table).query(table, "get_options", (), True)
        return table, req

    def vacuum_options( self ):
        table = "option"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def save_directory( self, parent_id, device, name ):
        """
        @rtype: int
        """
        table = "directory"
        req = self.getTable(table).query(table, "save_directory", (parent_id, device, name,), True)
        return table, req

    def get_file_full_path( self, item ):
        table = "directory"
        req = self.getTable(table).query(table, "get_file_full_path", (item,), True)
        return table, req

    def vacuum_directory( self ):
        table = "directory"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def insert_file( self, parent_id, device, mime, size, mtime, name ):
        """
        @rtype: int
        """
        table = "file"
        req = self.getTable(table).query(table, "insert_file", (parent_id, device, mime, size, mtime, name,), True)
        return table, req

    def remove_file( self, file_id ):
        table = "file"
        req = self.getTable(table).query(table, "remove_file", (file_id,), False)
        return table, req

    def get_file( self, file_id ):
        table = "file"
        req = self.getTable(table).query(table, "get_file", (file_id,), True)
        return table, req

    def vacuum_file( self ):
        table = "file"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def insert_hash( self, file_hash ):
        table = "hash"
        req = self.getTable(table).query(table, "insert_hash", (file_hash,), True)
        return table, req

    def find_hash( self, file_hash ):
        table = "hash"
        req = self.getTable(table).query(table, "find_hash", (file_hash,), True)
        return table, req

    def vacuum_hash( self ):
        table = "hash"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def link_hash( self, file_id, hash_id ):
        table = "file"
        req = self.getTable(table).query(table, "link_hash", (file_id, hash_id,), True)
        return table, req

    def get_file_hash_id( self, file_id ):
        table = "file"
        req = self.getTable(table).query(table, "get_file_hash_id", (file_id,), True)
        return table, req

    def get_nonuniq_hash_ids( self ):
        table = "file"
        req = self.getTable(table).query(table, "get_nonuniq_hash_ids", (), True)
        return table, req

    def get_file2hash_by_hash_ids( self, hash_ids ):
        table = "file"
        req = self.getTable(table).query(table, "get_file2hash_by_hash_ids", (hash_ids,), True)
        return table, req

    def get_files_by_ids( self, file_ids ):
        table = "file"
        req = self.getTable(table).query(table, "get_by_ids", (file_ids,), True)
        return table, req

    def get_hashes_by_ids( self, hash_ids ):
        table = "hash"
        req = self.getTable(table).query(table, "get_by_ids", (hash_ids,), True)
        return table, req


    def insert_image( self, file_id, mime, size, is_gray,
                      width, height ):
        """
        @rtype: int
        """
        table = "image"
        req = self.getTable(table).query(table, "insert_image", (
            file_id, mime, size, is_gray, width, height,), True)
        return table, req

    def get_image(self, image_id):
        table = "image"
        req = self.getTable(table).query(table, "get_image", (image_id,), True)
        return table, req

    def get_images_count(self):
        table = "image"
        req = self.getTable(table).query(table, "get_images_count", (), True)
        return table, req

    def get_images_in_file_count(self, file_id):
        table = "image"
        req = self.getTable(table).query(table, "get_images_in_file_count", (file_id,), True)
        return table, req

    def remove_file_images(self, file_id):
        table = "image"
        req = self.getTable(table).query(table, "remove_file_images", (file_id,), False)
        return table, req

    def get_images(self, count, offset):
        table = "image"
        req = self.getTable(table).query(table, "get_images", (count, offset,), True)
        return table, req

    def vacuum_image( self ):
        table = "image"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req



    def insert_image_v10( self, file_id, image_id, orientation,
                          v0, v1, v2, v3, v4, v5, v6, v7, v8, v9 ):
        table = "image_vector10"
        req = self.getTable(table).query(table, "insert_image_v10", (
            file_id, image_id, orientation, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9,), False)
        return table, req

    def remove_image_vectors10_by_file(self, file_id):
        table = "image_vector10"
        req = self.getTable(table).query(table, "remove_file_images", (file_id,), False)
        return table, req

    def get_image_vectors10(self, image_id):
        table = "image_vector10"
        req = self.getTable(table).query(table, "get_image_vectors", (image_id,), True)
        return table, req

    def get_similar_images10(self, image_vector):
        table = "image_vector10"
        req = self.getTable(table).query(table, "get_similar_images", (image_vector,), True)
        return table, req

    def vacuum_image_vector10( self ):
        table = "image_vector10"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def insert_image_v100( self, file_id, image_id, orientation, vector ):
        table = "image_vector100"
        req = self.getTable(table).query(table, "insert_image_v100", (file_id, image_id, orientation, vector,), False)
        return table, req

    def get_image_vectors100(self, image_id):
        table = "image_vector100"
        req = self.getTable(table).query(table, "get_image_vectors", (image_id,), True)
        return table, req

    def remove_image_vectors100_by_file(self, file_id):
        table = "image_vector100"
        req = self.getTable(table).query(table, "remove_file_images", (file_id,), False)
        return table, req

    def vacuum_image_vector100( self ):
        table = "image_vector100"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def clean_image2image(self):
        table = "image2image"
        req = self.getTable(table).query(table, "clean", (), True)
        return table, req

    def has_image2image(self, image1_id, image2_id):
        table = "image2image"
        req = self.getTable(table).query(table, "has_image2image", (image1_id, image2_id,), True)
        return table, req

    def insert_image2image( self, image1_id, image2_id, file1_id, file2_id, equal_proc, quality_points):
        table = "image2image"
        req = self.getTable(table).query(table, "insert_image2image", (image1_id, image2_id, file1_id, file2_id, equal_proc, quality_points,), True)
        return table, req

    def remove_file_image2image(self, file_id):
        table = "image2image"
        req = self.getTable(table).query(table, "remove_file_file2file", (file_id,), True)
        return table, req

    def vacuum_image2image( self ):
        table = "image2image"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req


    def clean_file2file(self):
        table = "file2file"
        req = self.getTable(table).query(table, "clean", (), True)
        return table, req

    def has_file2file(self, file1_id, file2_id):
        table = "file2file"
        req = self.getTable(table).query(table, "has_file2file", (file1_id, file2_id,), True)
        return table, req

    def insert_file2file( self, file1_id, file2_id, file1_count, file2_count):
        table = "file2file"
        req = self.getTable(table).query(table, "insert_file2file", (file1_id, file2_id, file1_count, file2_count,), True)
        return table, req

    def inc_file2file( self, f2f_id, quality):
        table = "file2file"
        req = self.getTable(table).query(table, "inc_file2file", (f2f_id, quality,), True)
        return table, req

    def get_equal_files(self, diff):
        table = "file2file"
        req = self.getTable(table).query(table, "get_equal_files", (diff,), True)
        return table, req

    def get_file2file_count(self):
        table = "file2file"
        req = self.getTable(table).query(table, "get_count", (), True)
        return table, req

    def get_file2file(self, count, offset):
        table = "file2file"
        req = self.getTable(table).query(table, "get_file2file", (count, offset,), True)
        return table, req

    def remove_file_file2file(self, file_id):
        table = "file2file"
        req = self.getTable(table).query(table, "remove_file_file2file", (file_id,), True)
        return table, req

    def vacuum_file2file( self ):
        table = "file2file"
        req = self.getTable(table).query(table, "vacuum", (), False)
        return table, req

    pass
